package com.nimt.hqgl_idea.mapper;


import com.nimt.hqgl_idea.entity.Goods;
import com.nimt.hqgl_idea.entity.GoodsUseRecord;
import com.nimt.hqgl_idea.vo.GoodsVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "goodsMapper")
public interface GoodsMapper {
    @Select("<script>" +
            "select * from goods " +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='keywords!=null'> and concat(id,name) like concat('%',#{keywords},'%')</if>" +
            "</trim>" +
            " limit #{start},#{pageSize}" +
            "</script>")
    List<Goods> selectAll(@Param("start") Integer start,
                          @Param("pageSize") Integer pageSize,
                          @Param("keywords") String keywords);


    @Select("<script>" +
            "select count(1) from goods " +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='keywords!=null'> and name like concat('%',#{keywords},'%')</if>" +
            "</trim>" +
            "</script>")
    int count(@Param("keywords") String keywords);

    /*@Update("<script>" +
            "update goods " +
            "<trim prefix='set' suffix='where id=#{id}' suffixOverrides=','>" +
            "<if test='quantity!=null'> stock=stock+#{quantity},</if>" +
            "</trim>" +
            "</script>")
    int update(GoodsVo goodsVo);*/

    @Update("<script>" +
            "update goods " +
            "<trim prefix='set' suffix='where id=#{id}' suffixOverrides=','>" +
            "<if test='name!=null'> name=#{name},</if>" +
            "<if test='price!=null'> price=#{price},</if>" +
            "<if test='remark!=null'> remark=#{remark},</if>" +
            "<if test='stock!=null'> stock=#{stock},</if>" +
            "</trim>" +
            "</script>")
    int update(Goods goods);

    @Insert("insert into goods_use_record values (null,#{goodsId},#{userId},#{quantity},now())")
    int insertGoodsUseRecord(GoodsUseRecord record);

    @Select("select * from goods where id = #{id}")
    Goods selectById(@Param("id") Integer id);

    @Insert("insert into goods values(null,#{name},#{stock},#{price},#{remark})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(Goods goods);

    @Delete("delete from goods where id = #{id}")
    int delete(Integer id);
}
